package com.laicunba.ext.activity.act20170526;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.laicunba.event.ActivityJoinEvent;
import com.laicunba.pojo.ActivityDO;
import com.laicunba.pojo.ActivityJoinDO;
import com.laicunba.pojo.CashCouponDefDO;
import com.laicunba.pojo.RateIncrCouponDefDO;
import com.laicunba.service.ActivityJoinService;
import com.laicunba.service.ActivityService;
import com.laicunba.service.CashCouponDefService;
import com.laicunba.service.RateIncrCouponDefService;
import com.laicunba.service.RegistryService;
import com.laicunba.service.VcoinWalletService;
import com.laicunba.util.AssertUtil;
import com.laicunba.util.StringUtil;

//@Component
public class Act20170526Listener implements ApplicationListener<ActivityJoinEvent> {
	@Autowired
	private VcoinWalletService vcoinWalletService;
	@Autowired
	private CashCouponDefService cashCouponDefService;
	@Autowired
	private RateIncrCouponDefService rateIncrCouponDefService;
	@Autowired
	private ActivityJoinService activityJoinService;
	@Autowired
	private RegistryService registryService;
	@Autowired
	private ActivityService activityService;

	@Override
	public void onApplicationEvent(ActivityJoinEvent event) {
		ActivityJoinDO joinDO = (ActivityJoinDO) event.getSource();
		if (!"20170526".equalsIgnoreCase(joinDO.getActivityId())) {
			return;
		}
		
		ActivityDO activity = activityService.get(ActivityDO.class, joinDO.getActivityId());
		if(activity == null || activity.getBeginTime().after(new Date()) || activity.getEndTime().before(new Date())) {
			return;
		}

		if (joinDO != null && StringUtil.isNotEmpty(joinDO.getUserId(), joinDO.getSubActId())) {
			if ("001".equals(joinDO.getSubActId())) {
				CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class, "act-dragon-boat-festival-red-package-5");
				if (couponDef != null) {
					cashCouponDefService.send2UserWithMessage(couponDef.getId(), joinDO.getUserId(), false,"端午节活动兑换现金券");
					joinDO.setRewardResp("端午节活动，积分兑换5元现金券");
					joinDO.setStatus("success");
					vcoinWalletService.subtract(joinDO.getUserId(), 300, "DRAGON_BOAT_FESTIVAL", "端午节活动期间成功兑换5元现金券");
				}
			} else if ("002".equals(joinDO.getSubActId())) {
				RateIncrCouponDefDO rateIncrDef = rateIncrCouponDefService.get(RateIncrCouponDefDO.class, "act-dragon-boat-festival-rate-incr-0.5");
				if (rateIncrDef != null) {
					rateIncrCouponDefService.send2User(rateIncrDef.getId(), joinDO.getUserId(), false, "端午节活动兑换加息券");
					joinDO.setRewardResp("端午节活动，积分兑换0.5%加息券");
					joinDO.setStatus("success");
					vcoinWalletService.subtract(joinDO.getUserId(), 300, "DRAGON_BOAT_FESTIVAL", "端午节活动期间成功兑换0.5%加息券");
				}
			} else if ("003".equals(joinDO.getSubActId())) {
				CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class, "act-dragon-boat-festival-red-package-55");
				if (couponDef != null) {
					cashCouponDefService.send2UserWithMessage(couponDef.getId(), joinDO.getUserId(), false,"端午节活动兑换现金券");
					joinDO.setRewardResp("端午节活动，积分兑换55元现金券");
					joinDO.setStatus("success");
					vcoinWalletService.subtract(joinDO.getUserId(), 5888, "DRAGON_BOAT_FESTIVAL", "端午节活动期间成功兑换55元现金券");
				}
			} else if ("004".equals(joinDO.getSubActId())) {
				joinDO.setRewardResp("端午节活动，积分兑换50元京东E卡");
				vcoinWalletService.subtract(joinDO.getUserId(), 5888, "DRAGON_BOAT_FESTIVAL", "端午节活动期间成功兑换50元京东E卡");
			} else if ("005".equals(joinDO.getSubActId())) {
				CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class, "act-dragon-boat-festival-red-package-200");
				if (couponDef != null) {
					cashCouponDefService.send2UserWithMessage(couponDef.getId(), joinDO.getUserId(), false,"端午节活动兑换现金券");
					joinDO.setRewardResp("端午节活动，积分兑换200元现金券");
					joinDO.setStatus("success");
					vcoinWalletService.subtract(joinDO.getUserId(), 20000, "DRAGON_BOAT_FESTIVAL", "端午节活动期间成功兑换200元现金券");
				}
			} else if ("006".equals(joinDO.getSubActId())) {
				joinDO.setRewardResp("端午节活动，积分兑换200元京东E卡");
				vcoinWalletService.subtract(joinDO.getUserId(), 20000, "DRAGON_BOAT_FESTIVAL", "端午节活动期间成功兑换200元京东E卡");
			} else if ("007".equals(joinDO.getSubActId())) {
				CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class, "act-dragon-boat-festival-red-package-500");
				if (couponDef != null) {
					cashCouponDefService.send2UserWithMessage(couponDef.getId(), joinDO.getUserId(), false,"端午节活动兑换现金券");
					joinDO.setRewardResp("端午节活动，积分兑换500元现金券");
					joinDO.setStatus("success");
					vcoinWalletService.subtract(joinDO.getUserId(), 41600, "DRAGON_BOAT_FESTIVAL", "端午节活动期间成功兑换500元现金券");
				}
			} else if ("008".equals(joinDO.getSubActId())) {
				joinDO.setRewardResp("端午节活动，积分兑换500元京东E卡");
				vcoinWalletService.subtract(joinDO.getUserId(), 41600, "DRAGON_BOAT_FESTIVAL", "端午节活动期间成功兑换500元京东E卡");
			} else if ("009".equals(joinDO.getSubActId())) {
				CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class, "act-dragon-boat-festival-red-package-3000");
				if (couponDef != null) {
					cashCouponDefService.send2UserWithMessage(couponDef.getId(), joinDO.getUserId(), true,"端午节活动兑换现金券");
					joinDO.setRewardResp("端午节活动，积分兑换3000元现金券");
					joinDO.setStatus("success");
					vcoinWalletService.subtract(joinDO.getUserId(), 220000, "DRAGON_BOAT_FESTIVAL", "端午节活动期间成功兑换3000元现金券");
				}
			} else if ("010".equals(joinDO.getSubActId())) {
				joinDO.setRewardResp("端午节活动，积分兑换Ipad mini 4（128G）");
				vcoinWalletService.subtract(joinDO.getUserId(), 220000, "DRAGON_BOAT_FESTIVAL", "端午节活动期间成功兑换Ipad mini 4");
			} else if ("011".equals(joinDO.getSubActId())) {
				joinDO.setRewardResp("端午节活动，积分兑换Iphone7（红色、128G）");
				vcoinWalletService.subtract(joinDO.getUserId(), 375000, "DRAGON_BOAT_FESTIVAL", "端午节活动期间成功兑换Iphone7");
			} else if ("012".equals(joinDO.getSubActId())) {
				joinDO.setRewardResp("端午节活动，积分兑换MacBook Air13.3英寸");
				vcoinWalletService.subtract(joinDO.getUserId(), 390000, "DRAGON_BOAT_FESTIVAL", "端午节活动期间成功兑换MacBook Air");
			}
			AssertUtil.assertTrue(activityJoinService.updateSkipCheckOwner(joinDO), "REWARD_FAIL");
		}
	}

}
